﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;

namespace WpfPdfReader
{
    /// <summary>
    /// WindowDetail.xaml 的交互逻辑
    /// </summary>
    public partial class WindowDetail : Window
    {
        private const double minImgHeight = 300;
        private const double minImgWidth = 300;

        public WindowDetail()
        {
            InitializeComponent();
        }

        public WindowDetail(FileItemViewModel fileItemViewModel)
            : this()
        {
            this.Loaded += new RoutedEventHandler(WindowDetail_Loaded);
            this.DataContext = fileItemViewModel;
            this.page.Text = string.Format("{0}", fileItemViewModel.Page + 1);     
        }

        void WindowDetail_Loaded(object sender, RoutedEventArgs e)
        {
            FileItemViewModel fileItemViewModel = this.DataContext as FileItemViewModel;
            System.Drawing.Bitmap map = System.Drawing.Bitmap.FromFile(fileItemViewModel.LastPageBmpPath) as System.Drawing.Bitmap;
            SetImage(map);
        }

        private void SetImage(System.Drawing.Bitmap image)
        {
            if (image != null)
            {
                BitmapImage bimage = new BitmapImage();
                bimage.BeginInit();
                MemoryStream ms = new MemoryStream();
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                bimage.StreamSource = ms;
                bimage.EndInit();

                if (image.Width >= image.Height)
                {
                    double factor = Convert.ToDouble(image.Width) / Convert.ToDouble(image.Height);
                    double deltax = this.Width - this.ClientSize.Width;
                    double deltay = this.Height - this.ClientSize.Height;

                    double minImgWidth = factor * minImgHeight;
                    double maxImgHeight = System.Windows.SystemParameters.WorkArea.Height - deltay;
                    double maxImgWidth = factor * maxImgHeight;
                    if (maxImgWidth + deltax > System.Windows.SystemParameters.WorkArea.Width)
                    {
                        maxImgWidth = System.Windows.SystemParameters.WorkArea.Width - deltax;
                        maxImgHeight = maxImgWidth / factor;
                        this.MaximumSize = new System.Windows.Size(System.Windows.SystemParameters.WorkArea.Width, maxImgHeight);
                    }
                    else
                    {
                        this.MaximumSize = new System.Windows.Size(maxImgWidth + deltax, System.Windows.SystemParameters.WorkArea.Height);
                    }
                    this.image.Source = bimage;
                    this.ClientSize = new Rect(0, 0, image.Size.Width, image.Size.Height);
                    this.MinimumSize = new System.Windows.Size(minImgWidth + deltax, minImgHeight + deltay);
                }
                else
                {
                    double factor = Convert.ToDouble(image.Height) / Convert.ToDouble(image.Width);
                    double deltax = this.Width - this.ClientSize.Width;
                    double deltay = this.Height - this.ClientSize.Height;

                    double minImgHeight = Convert.ToInt32(factor * minImgWidth);
                    double maxImgHeight = System.Windows.SystemParameters.WorkArea.Height - deltay;
                    double maxImgWidth = Convert.ToInt32(maxImgHeight / factor);

                    this.image.Source = bimage;
                    this.ClientSize = new Rect(0, 0, image.Size.Width, image.Size.Height);

                    this.MinimumSize = new System.Windows.Size(minImgWidth + deltax, minImgHeight + deltay);
                    this.MaximumSize = new System.Windows.Size(maxImgWidth + deltax, System.Windows.SystemParameters.WorkArea.Height);
                }

                double startx = (System.Windows.SystemParameters.WorkArea.Width - this.Width) / 2;
                double starty = (System.Windows.SystemParameters.WorkArea.Height - this.Height) / 2;
                this.Location = new System.Windows.Point(startx, starty);
            }
        }

        public Rect ClientSize
        {
            get
            {
                Rect clientSize = new Rect();
                clientSize.Width = this.ActualWidth - System.Windows.SystemParameters.ResizeFrameVerticalBorderWidth * 2;
                clientSize.Height = this.ActualHeight - System.Windows.SystemParameters.ResizeFrameHorizontalBorderHeight * 2 - System.Windows.SystemParameters.WindowCaptionHeight - this.stackPanel.ActualHeight;
                return clientSize;
            }
            set
            {
                this.Width = value.Width + System.Windows.SystemParameters.ResizeFrameVerticalBorderWidth * 2;
                this.Height = value.Height + System.Windows.SystemParameters.ResizeFrameHorizontalBorderHeight * 2 + System.Windows.SystemParameters.WindowCaptionHeight + this.stackPanel.ActualHeight;
            }
        }

        public System.Windows.Point Location
        {
            get
            {
                return new System.Windows.Point(this.Left, this.Top);
            }
            set
            {
                this.Left = value.X;
                this.Top = value.Y;
            }
        }

        public System.Windows.Size MinimumSize
        {
            get
            {
                return new System.Windows.Size(this.MinWidth, this.MinHeight);
            }
            set
            {
                this.MinWidth = value.Width;
                this.MinHeight = value.Height;
            }
        }

        public System.Windows.Size MaximumSize
        {
            get
            {
                return new System.Windows.Size(this.MaxWidth, this.MaxHeight);
            }
            set
            {
                this.MaxWidth = value.Width;
                this.MaxHeight = value.Height;
            }
        }
    }
}
